package com.guonima;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@SpringBootApplication
public class EurekaRibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaRibbonApplication.class, args);
	}

	/**
	 * LoadBalanced 注解 启动负载均衡 默认是 轮询调用
	 * @return
	 */
	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}

	@Bean
	RestTemplate restNoBalancedTemplate(){
		return new RestTemplate();
	}

	/**
	 * 更换负载均衡策略 随机访问
	 * @return
	 */
	@Bean
	public IRule ribbonRule() {
		return new RandomRule();//这里配置策略，和配置文件对应
	}
}
